package com.qf.servlet;

import com.qf.Enity.User;
import com.qf.service.UserserviceImpl;
import com.qf.service.Userservidce;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       /* req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");*/
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        UserserviceImpl userservice = new UserserviceImpl();
        User user = userservice.login(username, password);
        //做出响应
        if (user != null) {
           /* req.setAttribute("user",user);
            req.getRequestDispatcher("/show").forward(req,resp);*/
            //使用session域，存储登陆的对象
            HttpSession session = req.getSession();
            System.out.println("登陆界面的session："+session.getId() );
            session.setAttribute("user",user);
            resp.sendRedirect("/show");
            /*out.write("<html>");
            out.write("<head>");
            out.write("<title>主页</title>");
            out.write("</head>");
            out.write("<body>");
            out.write("<h1>登录成功,欢迎"+user.getUsername()+"</h1>");
            out.write("</body>");
            out.write("</html>");*/
        } else {
            /*out.write("<html>");
            out.write("<head>");
            out.write("<title>主页</title>");
            out.write("</head>");
            out.write("<body>");
            out.write("<h1 style='color:red'>登录失败,用户名或密码错误</h1>");
            out.write("</body>");
            out.write("</html>");*/
            /*req.getRequestDispatcher("/error.jsp").forward(req,resp);;*/
            resp.sendRedirect("/error.jsp");
        }
    }
}